<template>
	<view class="myInfoPage">
		<view class="infoTitle">
			账号信息
		</view>
		<u-cell-group :customStyle="cellStyle" :border="false">
			<u-cell title="个人设置">
				<view slot="value">
					<u--image :src="userInfo.headPic||avatarUrl" shape="circle" mode="aspectFill" width="120rpx"
						height="120rpx">
					</u--image>
				</view>
			</u-cell>
			<u-cell title="V+编号">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入姓名" readonly fontSize="28rpx"
					color="#666666FF" v-model="userInfo.userNum" :maxlength="10"></u--input>
			</u-cell>
			<u-cell title="姓名">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入姓名" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.realName" :maxlength="10"></u--input>
			</u-cell>
			<u-cell title="昵称">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入昵称" clearable fontSize="28rpx"
					color="#666666FF" :value="userInfo.nickname"></u--input>
			</u-cell>
			<u-cell title="性别" isLink @click="selectSex">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请选择性别" readonly fontSize="28rpx"
					color="#666666FF" :value="getSex(userInfo.sex)"></u--input>
			</u-cell>
			<u-cell title="生日" isLink @click="selectBirth">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请选择生日" readonly fontSize="28rpx"
					color="#666666FF" :value="getBirthday(userInfo.birthdayTime)"></u--input>
			</u-cell>
			<u-cell title="手机号码">
				<u--input type="idCard" slot="value" border="none" inputAlign="right" placeholder="请输入手机号码" clearable
					fontSize="28rpx" color="#666666FF" v-model="userInfo.phone" :maxlength="11"></u--input>
			</u-cell>
			<u-cell title="身份" isLink :border="false" @click="selectIdentity">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请选择身份" readonly fontSize="28rpx"
					color="#666666FF" :value="getRole(userInfo.role)" :maxlength="20"></u--input>
			</u-cell>
		</u-cell-group>
		<view class="infoTitle">
			更多信息
		</view>
		<u-cell-group :customStyle="cellStyle" :border="false" v-if="userInfo.role=='0'">
			<u-cell title="年级">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入年级" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.grade" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="专业">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入专业" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.major" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="学校">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入学校" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.school" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="教育邮箱" :border="false">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入邮箱" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.educationMailbox" :maxlength="50"></u--input>
			</u-cell>
		</u-cell-group>
		<u-cell-group :customStyle="cellStyle" :border="false" v-if="userInfo.role=='1'">
			<u-cell title="专业">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入专业" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.major" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="学校">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入学校" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.school" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="教育邮箱" :border="false">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入邮箱" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.educationMailbox" :maxlength="50"></u--input>
			</u-cell>
		</u-cell-group>
		<u-cell-group :customStyle="cellStyle" :border="false" v-if="userInfo.role=='2'">
			<u-cell title="企业名称">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入企业名称" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.corporateName" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="部门">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入部门" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.department" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="邮箱" :border="false">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入邮箱" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.educationMailbox" :maxlength="50"></u--input>
			</u-cell>
		</u-cell-group>
		<u-cell-group :customStyle="cellStyle" :border="false" v-if="userInfo.role=='3'">
			<u-cell title="从业年限">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入年限" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.actualOperation" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="职务">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入职务" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.post" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="公司">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入公司" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.corporateName" :maxlength="50"></u--input>
			</u-cell>
			<u-cell title="邮箱" :border="false">
				<u--input slot="value" border="none" inputAlign="right" placeholder="请输入邮箱" clearable fontSize="28rpx"
					color="#666666FF" v-model="userInfo.educationMailbox" :maxlength="50"></u--input>
			</u-cell>
		</u-cell-group>
		<view class="btnContent">
			<view class="myInfoBtn" @click="saveInfo">保存</view>
		</view>
		<u-action-sheet :safeAreaInsetBottom="true" :actions="sexList" :show="showSexSelect"
			@close="showSexSelect = false" @select="chooseSex">
		</u-action-sheet>
		<u-action-sheet :safeAreaInsetBottom="true" :actions="identityList" :show="showIdentitySelect"
			@close="showIdentitySelect = false" @select="chooseIdentity">
		</u-action-sheet>
		<u-datetime-picker :minDate="-2208988800000" :maxDate="dateNow" :closeOnClickOverlay="true"
			@close="showBirthSelect=false" @cancel="showBirthSelect=false" :show="showBirthSelect"
			:value="userInfo.birthdayTime||dateNow" mode="date" @confirm="chooseBirth"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		validPhone,
		validEmail
	} from "@/utils/common.js"
	export default {
		data() {
			return {
				dateNow: new Date().getTime(),
				showSexSelect: false,
				showBirthSelect: false,
				showIdentitySelect: false,
				avatarUrl: "",
				nickName: "",
				cellStyle: {
					padding: "0 20rpx",
					background: "#fff"
				},
				userInfo: {},
				sexList: [{
					name: "男",
					value: "0"
				}, {
					name: "女",
					value: "1"
				}],
				identityList: [{
					name: "学生",
					value: "0"
				}, {
					name: "教师",
					value: "1"
				}, {
					name: "企业",
					value: "2"
				}, {
					name: "工程师",
					value: "3"
				}]
			}
		},
		computed: {
			getBirthday(date) {
				return function(date) {
					if (date) {
					if (typeof date === 'number') {
						return new Date(date).format("YYYY-MM-dd")
					} else {
						return new Date(date.replace(/\-/g, "/")).format("YYYY-MM-dd")
					}
					} else {
						return ""
					}
				}
			},
			getSex(data) {
				return function(data) {
					let str = ""
					this.sexList.map(item => {
						if (item.value === data) {
							str = item.name
						}
					})
					return str;
				}
			},
			getRole(data) {
				return function(data) {
					let str = ""
					this.identityList.map(item => {
						if (item.value == data) {
							str = item.name
						}
					})
					return str;
				}
			}
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (res) => {
					let info = res.data;
					this.avatarUrl = info.avatarUrl
					this.nickName = info.nickName
					this.$request.get(this.$api.getUserById, {
						id: res.data.id
					}).then(result => {
						this.userInfo = result.result
						uni.hideLoading()
					})
				}
			})
		},
		methods: {
			selectSex() {
				this.showSexSelect = true
			},
			chooseSex(data) {
				this.userInfo.sex = data.value
			},
			selectIdentity() {
				this.showIdentitySelect = true
			},
			chooseIdentity(data) {
				this.userInfo.role = data.value
			},
			selectBirth() {
				this.showBirthSelect = true
			},
			chooseBirth(data) {
				this.userInfo.birthdayTime = data.value
				this.showBirthSelect = false
			},
			saveInfo() {
				if (!this.userInfo.realName) {
					uni.showToast({
						icon: "none",
						title: "请输入真实姓名"
					})
					return;
				}
				if (!this.userInfo.nickname) {
					uni.showToast({
						icon: "none",
						title: "请输入昵称"
					})
					return;
				}
				if (!this.userInfo.sex && this.userInfo.sex != 0) {
					uni.showToast({
						icon: "none",
						title: "请选择性别"
					})
					return;
				}
				if (!this.userInfo.phone) {
					uni.showToast({
						icon: "none",
						title: "请输入手机号"
					})
					return;
				}
				if (!validPhone(this.userInfo.phone)) {
					uni.showToast({
						icon: "none",
						title: "手机号格式输入有误"
					})
					return;
				}
				if (this.userInfo.educationMailbox && !validEmail(this.userInfo.educationMailbox)) {
					uni.showToast({
						icon: "none",
						title: "邮箱格式输入有误"
					})
					return;
				}
				let saveData = {
					id: this.userInfo.id,
					realName: this.userInfo.realName,
					nickname: this.userInfo.nickname,
					sex: this.userInfo.sex,
					birthdayTime: this.userInfo.birthdayTime,
					phone: this.userInfo.phone,
					role: this.userInfo.role,
				}
				if (this.userInfo.role == '0') {
					saveData.grade = this.userInfo.grade
					saveData.major = this.userInfo.major
					saveData.school = this.userInfo.school
					saveData.educationMailbox = this.userInfo.educationMailbox
				} else if (this.userInfo.role == '1') {
					saveData.major = this.userInfo.major
					saveData.school = this.userInfo.school
					saveData.educationMailbox = this.userInfo.educationMailbox
				} else if (this.userInfo.role == '2') {
					saveData.corporateName = this.userInfo.corporateName
					saveData.department = this.userInfo.department
					saveData.educationMailbox = this.userInfo.educationMailbox
				} else if (this.userInfo.role == '3') {
					saveData.actualOperation = this.userInfo.actualOperation
					saveData.post = this.userInfo.post
					saveData.corporateName = this.userInfo.corporateName
					saveData.educationMailbox = this.userInfo.educationMailbox
				}
				this.$request.post(this.$api.editUserInfo, saveData).then(res => {
					uni.navigateBack({

					})
					uni.hideLoading()
					setTimeout(() => {
						uni.showToast({
							icon: "success",
							title: "保存成功"
						})
					},300)
				})
			}
		}
	}
</script>

<style lang="scss">
	.myInfoPage {
		.infoTitle {
			background-color: #EEEEEE;
			height: 88rpx;
			line-height: 88rpx;
			position: relative;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
			font-size: 26rpx;

			&:before {
				display: inline-block;
				content: "";
				height: 30rpx;
				width: 8rpx;
				margin-right: 16rpx;
				position: relative;
				left: 0rpx;
				top: 6rpx;
				background-color: #004DA1FF;
			}
		}

		.btnContent {
			display: flex;
			justify-content: center;
			padding: 40rpx 0;
		}

		.myInfoBtn {
			background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
			border-radius: 48rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			font-size: 34rpx;
			height: 96rpx;
			width: 680rpx;
			text-align: center;
			line-height: 96rpx;
			letter-spacing: 10rpx;
		}
	}
</style>
